package com.zs.juc.thread;

/**
 * @author madison
 * @description
 * @date 2021/5/16 21:28
 */
public class JoinTest {


    public static void main(String[] args) throws InterruptedException {
        test();
//        test1();
    }


    public static void test() throws InterruptedException {

        System.out.println(Thread.currentThread().getName() + "线程运行开始!");
        Thread thread1 = new Thread(() -> {
            try {
                System.out.println("子线程开始执行");
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        thread1.setName("线程 B");
        thread1.start();
        thread1.join();
        System.out.println("这时 thread1 执行完毕之后才能执行主线程");
    }

    // 1.现在有T1、T2、T3三个线程，你怎样保证T2在T1执行完后执行，T3在T2执行完后执行
    public static void test1() {
        final Thread t1 = new Thread(() -> System.out.println("t1"));
        final Thread t2 = new Thread(() -> {
            try {
                // 引用t1线程，等待t1线程执行完
                t1.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("t2");
        });
        Thread t3 = new Thread(() -> {
            try {
                // 引用t2线程，等待t2线程执行完
                t2.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("t3");
        });
        //这里三个线程的启动顺序可以任意，大家可以试下！
        //经过测试好像不是按顺序
        t1.start();
        t2.start();
        t3.start();
    }
}
